<?php

namespace app\mobile\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;

class Shukan extends Frontend
{

    protected $noNeedLogin = ['index', 'qikan', 'qikan_detail', 'qikanqishu_detail', 'qikanwenzhang_detail', 'wenzhang_download', 'wenzhang_read', 'shuji', 'shuji_detail'];
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $banner = Db::name('banner')->where([
            'type_data' => 1,
            'bannertype_id' => 9
        ])->find();
        $this->assign('banner', $banner);

        $rs_qikan = Db::name('qikan')->where(['show_switch' => 1])->order('weigh desc')->limit(10)->select();
        $this->assign('rs_qikan', $rs_qikan);

        $rs_shuji = Db::name('shuji')->order('weigh desc')->limit(10)->select();
        $this->assign('rs_shuji', $rs_shuji);


        return $this->view->fetch();
    }

    public function qikan()
    {
        $rs_qikan = Db::name('qikan')->where(['show_switch' => 1])->order('weigh desc')->paginate(10);
        $this->assign('rs_qikan', $rs_qikan);
        return $this->view->fetch();
    }

    public function qikan_detail(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikan')->where(['id' => $id])->find();

        if (is_null($rs)) {
            $this->error('内容不存在');
        }
        $this->assign('rs', $rs);

        $years = Db::name('qikanqishu')->distinct(true)->where('qikan_id', $id)->order('year desc')->column('year');

        $this->assign('years', $years);
        $this->assign('year', $request->param('year'));

        if (empty($years)) {
            $this->error('该期刊未创建期数');
        }

        if ($request->has('year')) {
            $rs_qikan = Db::name('qikanqishu')->where([
                'qikan_id' => $id,
                'year' => $request->param('year')
            ])->order('qishu desc')->select();
        } else {
            $rs_qikan = Db::name('qikanqishu')->where([
                'qikan_id' => $id,
                'year' => $years[0]
            ])->order('qishu desc')->select();
        }

        $this->assign('rs_qikan', $rs_qikan);


        return $this->view->fetch();
    }

    public function qikan_dingyue(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikan')->where(['id' => $id])->find();

        if (is_null($rs)) {
            $this->error('内容不存在');
        }
        $this->assign('rs', $rs);


        return $this->view->fetch();
    }

    public function qikan_dingyue_dingdan(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikandingyue')->alias('d')->join('qikan q', 'q.id=d.qikan_id', 'LEFT')->field('d.*,d.image as d_image,q.name,q.image')->where(['d.id' => $id, 'user_id' => $this->auth->id])->find();

        if (is_null($rs)) {
            $this->error('内容不存在');
        }

        $this->assign('rs', $rs);

        return $this->view->fetch();
    }

    public function qikan_dingyue_fukuan(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikandingyue')->where(['id' => $id, 'user_id' => $this->auth->id])->find();

        if (is_null($rs)) {
            $this->error('订单不存在');
        }

        $this->assign('rs', $rs);


        return $this->view->fetch();
    }

    public function qikan_tougao()
    {
        $banner = Db::name('banner')->where([
            'type_data' => 1,
            'bannertype_id' => 9
        ])->find();
        $this->assign('banner', $banner);

        return $this->view->fetch();
    }

    public function qikanqishu_detail(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikanqishu')->alias('s')->where(['s.id' => $id])->field('s.*,q.name as qikan_name')
            ->join('qikan q', 's.qikan_id=q.id', 'LEFT')->find();

        $this->assign('rs', $rs);

        $rs_type = Db::name('qikanwenzhangtype')->where(['qikan_id' => $rs['qikan_id'], 'qikanqishu_id' => $id, 'show_switch' => 1])->order('weigh desc')->select();
        foreach ($rs_type as $k => $v) {
            $rs_type[$k]['docs'] = Db::name('qikanwenzhang')->where([
                'qikanwenzhangtype_id' => $v['id'],
                'qikanqishu_id' => $id,
                'qikan_id' => $v['qikan_id'],
            ])->order('weigh desc')->select();
        }
        $this->assign('rs_type', $rs_type);

        return $this->view->fetch();
    }

    public function qikanwenzhang_detail(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikanwenzhang')->where(['id' => $id])->find();

        $this->assign('rs', $rs);


        return $this->view->fetch();
    }

    public function wenzhang_download(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikanwenzhang')->where(['id' => $id])->find();
        $file = ROOT_PATH . 'public/' . $rs['content_file'];  // 待下载的文件路径
        $filename = basename($file);  // 获取文件名
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }

    public function wenzhang_read(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('qikanwenzhang')->where(['id' => $id])->find();
        $this->redirect($rs['content_file']);
    }

    public function shuji()
    {

        $rs_shuji = Db::name('shuji')->order('weigh desc')->paginate(10);
        $this->assign('rs_shuji', $rs_shuji);

        return $this->view->fetch();
    }

    public function shuji_detail(Request $request)
    {
        $id = $request->param('id/d', 0);
        $rs = Db::name('shuji')->where(['id' => $id])->find();
        $this->assign('rs', $rs);

        return $this->view->fetch();
    }
}
